<?php

require('config.php');

$page = new rootLayoutPage();
$page->addJavaScript('jquery.validate.js');
$page->addStyleSheet('jquery.validate.css');
$page->setBaliseTitle('contact_balise');
$page->setDescription('contact_description');

$xml = tmxTranslate('contact.xml', REP_XLAT_TEMPLATE);
$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
//-- menu Left show products--------------------------------//
//$_SERVER['QUERY_STRING']==''?$get='':$get='?'.$_SERVER['QUERY_STRING'];
	//$newsByTypeLink = modelNews::getNewsByUrl('contact.php'.$get);
	/*$id_menu_left_tree = modelNews::getNewsMenuleft($newsByTypeLink->language_link);
	$menu = new layoutMenuLeft();
	$menuPage = new layoutMenuPage();
	$menuPage->setTable('zz_data_url');
	$menuPage->setIdShow($id_menu_left_tree->id_menu_left_tree);
	$menu->pushMenu($menuPage);
	$page->setMenuPage($menu);*/

//--------Menu indicator--------------//
	/*$menuIndictor = new layoutMenuIndicator();
	$menuIndictor->idTree($id_menu_left_tree->id_menu_left_tree);
	$menuIndictor->title($id_menu_left_tree->name);
	$page->setMenuIndicator($menuIndictor);*/

//--------Menu Header-----------------//
	/*$header = new layoutHeader();
	$header->setArrayUrl($arr);
	$header->setIdShow($id_menu_left_tree->id_menu_left_tree);
	$page->setHeader($header);*/
		
$tpl_file='contact.tpl';
$contact = new flyLayout(REP_TPL . $tpl_file);
$contact->start();
//$contact->setVariable('title_1', $xml['title_1_1']);
	/*if(isset($_SESSION['id_user'])) {
		$user = modelUser::getUser($_SESSION['id_user']);
		$contact->setVariable('your_email', $user->login);
		$contact->showBlock('connection_with_email');
	}else{
		$contact->showBlock('not_connection');
	}*/
	$contact->showBlock('not_connection');
	if(isset($_POST['your_email']) and session_id()) {
		
		if(!isset($_SESSION['reporting'])) $_SESSION['reporting'] = array();
			//every post
			
		$stack = count($_SESSION['reporting']);				
		$getTotalTime = array();
		while ($stack>0 ) {
			if($_SESSION['reporting'][$stack-1]<(time()-CONTACT_TIME_MIN)){
				break;
			} 
			array_push($getTotalTime, $_SESSION['reporting'][$stack-1]);
			$stack--;
		}
		//echo count($getTotalTime);
		if(count($getTotalTime)<MAX_CONTACT_DURING_CONTACT_TIME){
			array_push($_SESSION['reporting'], time());
			
			$your_email = trim(addslashes($_POST['your_email']));
			$your_name = trim(addslashes($_POST['your_name']));
			$your_subject = trim(addslashes($_POST['your_subject']));
			$your_tel = trim(addslashes($_POST['your_tel']));
		    $text_message = nl2br(trim(addslashes($_POST['message_send'])));
		    //$subject = $xml_subject['contact'];
		    $headers=$your_name.' '.$your_email;
	    	$mail = new flyLayout(REP_MAIL.'contact.mail.tpl');
	    	$mail->start();
	    	$mail->setVariable('subject', $your_subject); 
	    	$mail->setVariable('telephone', $your_tel);    
	    	$mail->setVariable('text_message', $text_message);
	    	$mail->stop();
	    	
		    if(isValidEmail($your_email)) {
			    if(STATUS_EMAIL == 0) {
		    		echo $mail;
		    	}else if(STATUS_EMAIL == 1) {
		    		monsterMail(SEND_TO, $your_subject, $mail,'',$your_email);
		    	}else {
		    		echo $mail;
		    		monsterMail(SEND_TO, $your_subject, $mail,'',$your_email);
		    	}
						    	
				if(isset($_SESSION['id_user']))modelReportingDiscussion::newDiscussion($subject, $text_message, $_SESSION['id_user'], modelReportingDiscussion::ETAT_RETOUR);
		    	
		    	redirect('contact','done');
		    	
			}
		    else { // message d'erreur, mail invalide
		        Persistence::register(PERSIST_MESSAGE, new Message('L\'adresse email saisie n\'est pas valide', Message::ERROR));
		    }
		}else{
			Persistence::register(PERSIST_MESSAGE, new Message($xml['can_not_send_message'], Message::ERROR)); 
		}
	}
		
		
	//** get contact from zz_data_contact  ***//
	/*$str = $_SERVER['QUERY_STRING'];
	$sele=0;
	$getListContact = modelReportingMessageType::getListContact();	
	$_SERVER['QUERY_STRING']==''?$keyContact='':$keyContact=$_SERVER['QUERY_STRING'];
	$_SERVER['QUERY_STRING']==''?'':$contact->setVariable('text_contact', $xml[$str]);;
	if($getListContact->size() > 0){
		foreach($getListContact as $key => $connect){
			if($connect->key==$keyContact){ 
				$contact->setVariable('selected','selected="selected"');
				$sele = 1;
			}
			else $contact->setVariable('selected','');
			$type = 'title_'.$_SESSION['language_code'];
			$contact->setVariable('type', $connect->$type);
			$email = 'email_'.$_SESSION['language_code'];
			$contact->setVariable('type_contact', $connect->$email);
			$contact->setVariable('key_contact', $connect->key);		
			$contact->parseList('list_type_contact');
		}
	}*/
	//if($sele !=1 )  $contact->setVariable('selected_o','selected="selected"');
	if(isset($_GET['done'])){
    	$contact->setVariable('message_successful', $xml['message_successful']);
    	//$contact->setVariable('title_1', $xml['title_1']);
		$contact->showBlock('not_connection');
		//$contact->hideBlock('connection_with_email');
    	$contact->showBlock('message_successful');
	}

$contact->stop();

$page->setBody($contact);
$page->display();

?>